package com.roast.services.my;

import com.roast.util.DBBroker;

public class GeneralSO {

    protected static DBBroker dbbr;
    protected static int signal;
    static boolean isDBOpen = false;

    protected final int generalExecution(Object obj) {
        if (!openDataBase()) {
            return signal;
        }

        openSession();
        beginTransaction();

        signal = executeSO(obj);
        checkExecute();

        closeSession();
        closeDataBase();

        System.out.println("[GeneralSO:generalExecution:[" + signal + "]]");
        return signal;
    }

    private boolean openDataBase() {
        if (isDBOpen == false) {
            dbbr = new DBBroker();
            signal = dbbr.openDataBase();
            if (!generalOperationState(signal)) {
                return false;
            }
        }
        isDBOpen = true;
        return true;
    }

    private boolean openSession() {
        int signal1 = dbbr.openSession();
        return generalOperationState(signal1);
    }

    private boolean beginTransaction() {
        int signal1 = dbbr.beginTransaction();
        return generalOperationState(signal1);
    }

    //Override in child class
    protected int executeSO(Object obj) {
        return 0;
    }

    protected boolean executeState(int signal) {
        return false;
    }

    private boolean checkExecute() {
        int signal1;

        if (executeState(signal)) {
            signal1 = dbbr.commit();
            if (!generalOperationState(signal1)) {
                return false;
            }
        } else {
            signal1 = dbbr.rollback();
            if (!generalOperationState(signal1)) {
                return false;
            }
        }
        return true;
    }

    private boolean closeSession() {
        int signal1 = dbbr.closeSession();
        return generalOperationState(signal1);
    }

    private boolean closeDataBase() {
        int signal1 = dbbr.closeDataBase();
        if (!generalOperationState(signal1)) {
            return false;
        }
        isDBOpen = false;
        return true;
    }

    private boolean generalOperationState(int signal) {
        switch (signal) {
            case 10001:
                showOperationState(signal, "DBBroker:openDataBase:FAIL.");
                return false;
            case 10002:
                showOperationState(signal, "DBBroker:openDataBase:OK.");
                return true;
            case 10003:
                showOperationState(signal, "DBBroker:closeDataBase:FAIL.");
                return false;
            case 10004:
                showOperationState(signal, "DBBroker:closeDataBase:OK.");
                return true;
            case 10005:
                showOperationState(signal, "DBBroker:openSession:FAIL.");
                return false;
            case 10006:
                showOperationState(signal, "DBBroker:openSession:OK.");
                return true;
            case 10007:
                showOperationState(signal, "DBBroker:closeSession:FAIL.");
                return false;
            case 10008:
                showOperationState(signal, "DBBroker:closeSession:OK.");
                return true;
            case 10009:
                showOperationState(signal, "DBBroker:beginTransaction:FAIL.");
                return false;
            case 10010:
                showOperationState(signal, "DBBroker:beginTransaction:OK.");
                return true;
            case 100011:
                showOperationState(signal, "DBBroker:commit:FAIL.");
                return false;
            case 100012:
                showOperationState(signal, "DBBroker:commit:OK.");
                return true;
            case 100013:
                showOperationState(signal, "DBBroker:rollback:FAIL.");
                return false;
            case 100014:
                showOperationState(signal, "DBBroker:rollback:OK.");
                return true;
        }
        return false;
    }

    protected final void showOperationState(int signal, String state) {
        System.out.println("[SIGNAL: " + signal + " | STATE: " + state + "]");
    }
}
